package com.shundr.shipper.truck;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.shundr.shipper.R;
import com.shundr.shipper.base.BaseActivity;
import com.shundr.shipper.common.util.DialogUtil;
import com.shundr.shipper.common.util.LImageLoader;
import com.shundr.shipper.common.util.ProgressDialogHelper;
import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.common.view.DialogDisplay.OnDisplayListener;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.truck.http.UserTruckRequest;
import com.shundr.shipper.truck.model.TruckInfo;

/**
 * 添加车辆
 * AddTruckActivity
 * 
 * @author larry 2015-4-27 下午2:30:31 
 * @version 1.0.0
 *
 */
public class MyTruckDetailActivity extends BaseActivity implements OnClickListener{

	private LinearLayout mLayoutBottom;
	
	private ImageView ivFull;//车辆照片
	private ImageView ivDriverCard;//行驶证
	
	private TextView tvCarCode;
	private TextView tvCarType;
	private TextView tvCarLength;
	private TextView tvCarWeight;
	private TextView tvCurrentCity;
	private TextView tvDirection;
	private TextView tvTime;
	private TextView tvContactor;
	
	private Button btnStatus;
	
	private ImageLoader mImageLoader;
	
	private int mId;
	
	private TruckInfo mInfo;
	
	private Handler mHandler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case UserTruckRequest.ADD_TRUCK_SUCCESSED:
					ToastHelper.toastShort(mContext, msg.obj.toString());
					ProgressDialogHelper.dismiss();
					break;
				case UserTruckRequest.GET_TRUCK_INFO_SUCCESSED:
					mInfo=(TruckInfo)msg.obj;
					initData(mInfo);
					ProgressDialogHelper.dismiss();
					break;
				case UserTruckRequest.DELETE_TRUCK_SUCCESSED:
					ProgressDialogHelper.dismiss();
					ToastHelper.toastShort(mContext, msg.obj.toString());
					new Timer().schedule(new TimerTask() {
						
						@Override
						public void run() {
							setResult(888);
							finish();
						}
					}, 600);
					break;
				case UserTruckRequest.UPDATE_TRUCK_STATUS_SUCCESSED:
					ProgressDialogHelper.dismiss();
					int status = mInfo.getTruckStatus();
					if(status == 3){
						mInfo.setTruckStatus(4);
						btnStatus.setText(getResources().getString(R.string.truck_busying));
					}else if(status == 4){
						mInfo.setTruckStatus(3);
						btnStatus.setText(getResources().getString(R.string.truck_freeing));
					}
					setResult(888);
					break;
				case UserTruckRequest.REQUEST_FAILED:
					ProgressDialogHelper.dismiss();
					ToastHelper.toastShort(mContext, msg.obj.toString());
					break;
				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_truck_details);
		try {
			mId = getIntent().getIntExtra("id", -1);
			if(mId < 0){
				finish();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		initView();
		ProgressDialogHelper.show(mContext, "正在加载中...");
		new UserTruckRequest(mContext, mHandler).getTruckInfo(mId);
	}
	
	private void initView(){
		mLayoutBottom = (LinearLayout)findViewById(R.id.layout_bottom);
		tvCarCode=(TextView)findViewById(R.id.tv_car_code);
		tvCarType=(TextView)findViewById(R.id.tv_car_type);			
		tvCarLength=(TextView)findViewById(R.id.tv_car_length);
		tvCarWeight=(TextView)findViewById(R.id.tv_car_weight);
		tvCurrentCity=(TextView)findViewById(R.id.tv_current_city);
		tvDirection=(TextView)findViewById(R.id.tv_direction);
		tvTime = (TextView)findViewById(R.id.tv_time);
		tvContactor = (TextView)findViewById(R.id.tv_contactor);
		
		ivFull = (ImageView)findViewById(R.id.iv_car_full);
		ivDriverCard = (ImageView)findViewById(R.id.iv_driver);
		
		btnStatus = (Button)findViewById(R.id.btn_status);
		ivFull.setOnClickListener(this);
		ivDriverCard.setOnClickListener(this);
		
		initBottom();
		
		this.mImageLoader = ImageLoader.getInstance();
	}
	
	private void initBottom() {
		btnStatus.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						int status = mInfo.getTruckStatus();
						if(status == 3){
							new UserTruckRequest(mContext,mHandler).updateTruckStatus(mId,4);
						}else if(status == 4){
							new UserTruckRequest(mContext,mHandler).updateTruckStatus(mId,3);
						}
					}
				});
//		findViewById(R.id.btn_edit).setOnClickListener(
//				new OnClickListener() {
//
//					@Override
//					public void onClick(View v) {
//						startActivityForResult(new Intent(mContext, EditTruckActivity.class), 888);
//					}
//				});
	}
	private void initData(TruckInfo info){
		if(info == null){
			return;
		}
		try {
			
			int status = mInfo.getTruckStatus();
			if(status == 4){
				btnStatus.setText(mContext.getResources().getString(R.string.truck_busying));
				btnStatus.setTextColor(getResources().getColor(R.color.btn_orange_light));
			}else{
				btnStatus.setText(mContext.getResources().getString(R.string.truck_freeing));
				btnStatus.setTextColor(getResources().getColor(R.color.btn_green_light));
			}
			
			mImageLoader.displayImage(info.getTruckPic(), ivFull,LImageLoader.mOptions);
			mImageLoader.displayImage(info.getTruckLicensePic(), ivDriverCard,LImageLoader.mOptions);
			tvCarCode.setText(info.getTruckPlateNumber());
			if(StringUtil.isNull(info.getTruckType())){
				tvCarType.setVisibility(View.GONE);
			}else{
				tvCarType.setText(info.getTruckType());
				tvCarType.setVisibility(View.VISIBLE);
			}
			tvCarWeight.setText(info.getTruckCapacity() + " 吨");
			if(StringUtil.isEqual(info.getTruckLength(), "23")){
				tvCarLength.setText(info.getTruckLength() + " 米以上");
			}else{
				tvCarLength.setText(info.getTruckLength() + " 米");
			}
			tvCurrentCity.setText(info.getTruckCurrentCity());
			
			String direct = "";
			if(!StringUtil.isNull(info.getTruckForwardCity1())){
				direct = direct + info.getTruckForwardCity1() + ",";
			}
			if(!StringUtil.isNull(info.getTruckForwardCity2())){
				direct = direct + info.getTruckForwardCity2() + ",";
			}
			if(!StringUtil.isNull(info.getTruckForwardCity3())){
				direct = direct + info.getTruckForwardCity3() + ",";
			}
			if(!StringUtil.isNull(info.getTruckForwardCity4())){
				direct = direct + info.getTruckForwardCity4() + ",";
			}
			if(!StringUtil.isNull(direct)){
				direct = direct.substring(0, direct.length() - 1);
			}
			tvDirection.setText(direct);
			
			tvTime.setText(info.getTruckUpdateTime());
			tvContactor.setText(info.getTruckerName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onClick(View v) {
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.menu_confirm, menu);
		menu.getItem(0).setTitle("删除");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_confirm:
			DialogUtil.noticeAlertDialog(mContext, "删除", "是否确定删除此车辆？",
					new OnDisplayListener() {

						@Override
						public void confirm() {
							ProgressDialogHelper.show(mContext, "正在删除中...");
							new UserTruckRequest(mContext, mHandler).deleteTruck(mId);
						}

						@Override
						public void cancel() {

						}
					});
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		switch (resultCode) {
		case 888:
			new UserTruckRequest(mContext, mHandler).getTruckInfo(mId);
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onDestroy() {
//		releaseImageView(ivFull);
//		releaseImageView(ivDriverCard);
		super.onDestroy();
	}

}
